<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/apple-touch-icon.png">
  <link rel="icon" type="image/png" href="https://img.fungnotl.cn/basket.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  <title>原码,反码,补码详解 ~ fungnotl的博客</title>

  <link rel="stylesheet" href="/lib/font-awesome/css/all.min.css"  >
<link rel="stylesheet" href="/lib/bootstrap/css/bootstrap.min.css"  >
<link rel="stylesheet" href="/lib/mdbootstrap/css/mdb.min.css"  >
<link rel="stylesheet" href="/lib/github-markdown/github-markdown.min.css"  >
<link rel="stylesheet" href="//at.alicdn.com/t/font_1067060_qzomjdt8bmp.css">


  <link rel="stylesheet" href="/lib/prettify/tomorrow-night-eighties.min.css"  >

<link rel="stylesheet" href="/css/main.css"  >


  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.min.css"  >


</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>fungnotl的博客</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          <li class="nav-item">
            <a class="nav-link" href="/">主页</a>
          </li>
        
          
          
          <li class="nav-item">
            <a class="nav-link" href="/archives/">归档</a>
          </li>
        
          
          
          <li class="nav-item">
            <a class="nav-link" href="/tags/">标签</a>
          </li>
        
          
          
          <li class="nav-item">
            <a class="nav-link" href="/about/">我</a>
          </li>
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>


</nav>

    <div class="view intro-2" id="background"
         style="background: url('https://fungnotl-img.oss-cn-hangzhou.aliyuncs.com/index.jpg')no-repeat center center;
           background-size: cover;
           background-attachment: fixed;">
      <div class="full-bg-img">
        <div class="mask rgba-black-light flex-center">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              <br>
              
                <p class="mt-3">
                  <i class="fas fa-calendar-alt" aria-hidden="true"></i>&nbsp;
                  Friday, September 6th 2019, 7:42 am
                </p>
              

              <p>
                
                  
                  &nbsp;<i class="far fa-chart-bar"></i>
                  <span class="post-count">
                    903 字
                  </span>&nbsp;
                

                
                  
                  &nbsp;<i class="far fa-clock"></i>
                  <span class="post-count">
                      3 分钟
                  </span>&nbsp;
                

                
                  <!-- 不蒜子统计文章PV -->
                  
                  &nbsp;<i class="far fa-eye" aria-hidden="true"></i>&nbsp;
                  <span id="busuanzi_container_page_pv">
                    <span id="busuanzi_value_page_pv"></span> 次
                  </span>&nbsp;
                
              </p>
            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="py-5 z-depth-3" id="board">
        <div class="post-content mx-auto" id="post">
          <div class="markdown-body">
            <p>﻿首先从<strong>计算机中数值数据的编码和表示</strong>说起</p>
<h2 id="机器数和真值"><a href="#机器数和真值" class="headerlink" title="机器数和真值"></a>机器数和真值</h2><blockquote>
<p>实际运算中,数是有正负的,计算机中的数也有正负,通过用一个数的最高位表示符号,如果字长为8位,分别为D7-D0,那么D7就是符号位,0表示正数,1表示负数,D6-D0为数值位;</p>
</blockquote>
<blockquote>
<p>例如:</p>
<p>11010111 = -87</p>
</blockquote>
<p><strong>机器数</strong>:在计算机中,连同符号一起数码化的数,就被称为机器数;如上面的11010111;</p>
<p><strong>真值</strong>:使用正负号加其绝对值的表示方法的数值;如-87</p>
<h2 id="原码"><a href="#原码" class="headerlink" title="原码"></a>原码</h2><p>将最高位作为符号位（0表示正，1表示负），其它数字位代表数值本身的绝对值的数字;</p>
<p>如:</p>
<ul>
<li>数字6在计算机中原码表示为:     00000110</li>
<li>数字 -6  在计算机中原码表示为: 10000110</li>
</ul>
<blockquote>
<p>注意:</p>
<p>以上是在8位计算机中的原码表示，如果在32位或16位计算机中，表示方法是一样的，只是多了几个0而已.</p>
</blockquote>
<p>但是原码也是有缺陷的</p>
<p>有了数值的表示方法就可以对数进行算数运算，但是很快就发现用带符号位的原码进行乘除运算时结果正确，而<strong>在加减运算的时候会出现问题</strong>，如下：</p>
<pre><code class="java">1 - 1 = 1 + (-1) = 0

00000001 + 10000001 = 10000010 (-2)

显然是错误的</code></pre>
<p>这时候有人提出了反码</p>
<h2 id="反码"><a href="#反码" class="headerlink" title="反码:"></a>反码:</h2><p>反码表示规则为：如果是正数，则表示方法和原码一样，如果是负数，则保留符号位1，然后将这个数字的原码按照每位取反，则得到这个数的反码表示形式;</p>
<p>如:</p>
<ul>
<li>数字6在计算机中反码就是他的原码:0000 0110</li>
<li>数字(-6)在计算机中反码为: 1111 1001</li>
</ul>
<p>上文说到了原码的缺陷,在进行减法运算时所得结果与实际结果不相同;</p>
<p>那么反码就解决了减法运算计算的错误,不过还是存在缺陷,如下:</p>
<pre><code class="java">1 - 1 = 1 + (-1) = 0

0000 0001 + 1111 1110 = 1111 1111(-0)

有问题</code></pre>
<p>再看看其他减法是否出错:</p>
<pre><code class="java">1 - 2 = 1 + (-2) = (-1);

0000 0001 + 1111 1101 = 1111 1110(-1)

正确</code></pre>
<p><strong>说明反码在进行减法运算时是正确的,只有在结果为0时可能带有负号</strong>;</p>
<p>为什么会出现这样的问题呢?因为反码的表示范围为(-127-(-0)) (0-127),总共256个;</p>
<p>这个问题如何解决?这时候补码出来了!</p>
<h2 id="补码"><a href="#补码" class="headerlink" title="补码"></a>补码</h2><p>补码是计算机表示数据的一般方式(这句话很重要,圈起来,要考)</p>
<p>其规则为：</p>
<ul>
<li>如果是正数，则表示方法和原码一样</li>
<li>如果是负数，则将数字的反码加上1（相当于将原码数值按位取反然后在对地位加1）;</li>
</ul>
<p>负数的补码就是对反码加一，而正数不变，正数的原码反码补码都是一样的;</p>
<p>再来检验一下上面的问题:</p>
<pre><code class="java">1 - 1 = 1 + (-1) = 0;

0000 0001 + 1111 1111 = 0000 0000;


(正确)</code></pre>
<p>那为什么补码可以这样运算而其他的不行呢,</p>
<p>在补码中,<strong>用(-128)代替了(-0),所以补码的表示范围变成了(-128- 0-127),共256个</strong>;</p>
<p><strong>补码的设计目的</strong></p>
<ul>
<li><p>使符号位能与有效值部分一起参加运算,从而简化运算规则</p>
</li>
<li><p>使减法运算转换为加法运算,进一步简化计算机中运算器的线路设计</p>
</li>
</ul>
<p>这也就是为什么<strong>补码是计算机数据的一般表示方法</strong>的原因之一!</p>

            <hr>
          </div>
          <br>
          <div>
            <p>
            
            
              <span>
                <i class="iconfont icon-tag"></i>
                
                  <a class="hover-with-bg" href="/tags/%E5%8E%9F%E7%A0%81">原码</a>
                
                  <a class="hover-with-bg" href="/tags/%E8%A1%A5%E7%A0%81">补码</a>
                
                  <a class="hover-with-bg" href="/tags/%E5%8F%8D%E7%A0%81">反码</a>
                
              </span>
            
            </p>
            
              <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://zh.wikipedia.org/wiki/Wikipedia:CC_BY-SA_3.0%E5%8D%8F%E8%AE%AE%E6%96%87%E6%9C%AC" target="_blank" rel="nofollow noopener noopener">CC BY-SA 3.0协议</a> 。转载请注明出处！</p>
            
          </div>
        </div>
      </div>
    </div>
    <div class="d-none d-lg-block col-lg-2 toc-container">
      
  <div id="toc">
    <p class="h4"><i class="far fa-list-alt"></i>&nbsp;TOC</p>
    <div id="tocbot"></div>
  </div>

    </div>
  </div>
</div>

<!-- custom -->


<!-- Comments -->
<div class="col-lg-7 mx-auto nopadding-md">
  <div class="container comments mx-auto" id="comments">
    
      <br><br>
      
      
   <!-- 来必力City版安装代码 -->
<div id="lv-container" data-id="city" data-uid="MTAyMC80Nzg4My8yNDM4MA==">
	<script type="text/javascript">
   (function(d, s) {
       var j, e = d.getElementsByTagName(s)[0];

       if (typeof LivereTower === 'function') { return; }

       j = d.createElement(s);
       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       j.async = true;

       e.parentNode.insertBefore(j, e);
   })(document, 'script');
	</script>
<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
</div>
<!-- City版安装代码已完成 -->
  
  
    
  </div>
</div>

    
  </main>

  
    <a class="z-depth-1" id="scroll-top-button" href="#" role="button">
      <i class="fa fa-chevron-up scroll-top-arrow" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  <footer class="mt-5">
  <div class="text-center py-3">
    <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><b>Hexo</b></a>
    <i class="iconfont icon-love"></i>
    <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"> <b>Fluid</b></a>
    <br>

    
  
    <!-- 不蒜子统计PV -->
    
    &nbsp;<span id="busuanzi_container_site_pv">总访问量 
          <span id="busuanzi_value_site_pv"></span> 次</span>&nbsp;
  
  
    <!-- 不蒜子统计UV -->
    
    &nbsp;<span id="busuanzi_container_site_uv">总访客数 
            <span id="busuanzi_value_site_uv"></span> 人</span>&nbsp;
  
  <br>



    
  <!-- 备案信息 -->
  <a href="http://beian.miit.gov.cn/" target="_blank"
     rel="nofollow noopener">浙ICP备19040843号</a>
  


  </div>
</footer>

<!-- SCRIPTS -->
<script src="/lib/jquery/jquery.min.js" ></script>
<script src="/lib/popper/popper.min.js" ></script>
<script src="/lib/bootstrap/js/bootstrap.min.js" ></script>
<script src="/lib/mdbootstrap/js/mdb.min.js" ></script>
<script src="/js/main.js" ></script>


  <script src="/js/lazyload.js" ></script>



  
    <script src="/lib/tocbot/tocbot.min.js" ></script>
  
  <script src="/js/post.js" ></script>



  <script src="/lib/smooth-scroll/smooth-scroll.min.js" ></script>



  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>


<!-- Plugins -->


  

  

  

  

  




  <script src="/lib/prettify/prettify.min.js" ></script>
  <script>
    $(document).ready(function () {
      $('pre').addClass('prettyprint  linenums');
      prettyPrint();
    })
  </script>



  <script src="/lib/typed/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "原码,反码,补码详解&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 50,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script src="/lib/anchor/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "false",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      getSearchFile(path);
      this.onclick = null
    }
  </script>



  <script src="/lib/fancybox/jquery.fancybox.min.js" ></script>
  <script>
    $("#post img:not(.no-zoom img, img[no-zoom])").each(
      function () {
        var element = document.createElement("a");
        $(element).attr("data-fancybox", "images");
        $(element).attr("href", $(this).attr("src"));
        $(this).wrap(element);
      }
    );
  </script>





  





</body>
</html>
